package com.example.demo.mapper;

import com.example.demo.entity.AnNideshopGood;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.DTO.GoodsNewDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页商品表 Mapper 接口
 * </p>
 *
 * @author Mr.We
 * @since 2024-06-20
 */
@Mapper
public interface AnNideshopGoodMapper extends BaseMapper<GoodsNewDTO> {
    /**
     * 查询热门商品列表
     * 该方法用于从数据库或其它数据源中查询当前的热门商品列表。热门商品的定义和筛选逻辑可能基于多种因素，
     * 如销售量、用户浏览量、用户评价等。通过传递一个Map参数，可以灵活地根据不同的条件来查询热门商品。
     *
     * @param params 一个Map对象，包含查询所需的参数。参数的具体键值对取决于查询的实现和需求，
     *               可以用于筛选商品的条件，如类别、价格范围、销量等。
     * @return 返回一个AnNideshopGood类型的List，包含了查询结果中的热门商品。每个AnNideshopGood对象代表了一个商品，
     *         包含了商品的相关信息，如名称、图片、价格等。
     */
    @Results({
            @Result(property = "productId", column = "product_id")
    })
    @Select({
            "<script>",
            "SELECT a.*, b.id AS product_id",
            "FROM an_nideshop_good a",
            "LEFT JOIN af_nideshop_product b ON a.id = b.goods_id",
            "LEFT JOIN af_nideshop_product c ON a.id = c.goods_id AND c.retail_price > b.retail_price AND b.id != c.id",
            "WHERE 1 = 1 AND b.id IS NOT NULL AND c.id IS NULL AND a.is_delete != 1",
            "<if test='isNew != null'> AND a.is_new = #{params.isNew} </if>",
            "<if test='isHot != null'> AND a.is_hot = 1 </if>",
            "<if test='brandId != null'> AND a.brand_id = #{params.brandId} </if>",
            "<if test='name != null'> AND a.name LIKE CONCAT('%', #{params.name}, '%') </if>",
            "<if test='categoryId != null'> AND a.category_id = #{params.categoryId} </if>",
            "<if test='keyword != null'> AND a.keywords LIKE CONCAT('%', #{params.keyword}, '%') </if>",
            "<if test='categoryIds != null'>",
            "AND a.category_id IN ",
            "<foreach item='item' index='index' collection='categoryIds' open='(' separator=',' close=')'>",
            "#{params.item}",
            "</foreach>",
            "</if>",
            "<if test='goodsIds != null'>",
            "AND a.id IN ",
            "<foreach item='item' index='index' collection='goodsIds' open='(' separator=',' close=')'>",
            "#{params.item}",
            "</foreach>",
            "</if>",
            "<choose>",
            "<when test='sidx != null and sidx.trim() != \"\"'> ORDER BY ${params.sidx} ${params.order} </when>",
            "<otherwise> ORDER BY a.id DESC </otherwise>",
            "</choose>",
            "<if test='offset != null and limit != null'> LIMIT #{params.offset}, #{params.limit} </if>",
            "</script>"
    })
    List<GoodsNewDTO> queryHotGoodsList(Map<String, Object> params);
    /**
     * 根据给定的参数映射查询目录下的产品列表。
     * 该方法旨在提供一种灵活的方式，通过传递不同的参数来查询特定目录下的产品列表。
     * 参数映射可以包含各种条件，如目录ID、产品状态、价格范围等，以满足不同的查询需求。
     *
     * @param params 一个包含查询参数的映射表。每个参数的具体含义和使用取决于查询的需求。
     *               例如，可以包含目录ID来指定查询的目录，或者包含产品状态来过滤产品。
     * @return 返回一个AnNideshopGood类型的列表，代表了查询结果中的产品集合。
     *         每个AnNideshopGood对象代表了一个产品，包含了产品的详细信息，如名称、描述、价格等。
     */
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "listPicUrl", column = "list_pic_url"),
            @Result(property = "marketPrice", column = "market_price"),
            @Result(property = "retailPrice", column = "retail_price"),
            @Result(property = "goodsBrief", column = "goods_brief"),
            @Result(property = "productId", column = "product_id"),
            @Result(property = "orderNum", column = "order_num")
    })
    @Select("<script>"+
            "SELECT a.id, a.name, a.list_pic_url, a.market_price, a.retail_price, a.goods_brief, b.id AS product_id, COUNT(og.id) AS order_num"+
            "FROM an_nideshop_good a"+
            "LEFT JOIN af_nideshop_product b ON a.id = b.goods_id"+
            "LEFT JOIN af_nideshop_product c ON a.id = c.goods_id AND c.retail_price > b.retail_price AND b.id != c.id"+
            "LEFT JOIN AFshop_order_goods og ON og.product_id = b.id"+
            "<if test='isGroup != null and isGroup == true'>"+
            "LEFT JOIN nideshop_goods_group gg ON gg.goods_id = a.id"+
            "</if>"+
            "WHERE 1 = 1 AND b.id IS NOT NULL AND c.id IS NULL AND a.is_delete != 1"+
            "<if test='isNew != null'> AND a.is_new = #{params.isNew} </if>"+
            "<if test='isHot != null'> AND a.is_hot = #{params.isHot} </if>"+
            "<if test='brandId != null'> AND a.brand_id = #{params.brandId} </if>"+
            "<if test='name != null'> AND a.name LIKE CONCAT('%', #{params.name}, '%') </if>"+
            "<if test='categoryId != null'> AND a.category_id = #{params.categoryId} </if>"+
            "<if test='keyword != null'> AND a.keywords LIKE CONCAT('%', #{params.keyword}, '%') </if>"+
            "<if test='isGroup != null and isGroup == true'> AND gg.id IS NOT NULL </if>"+
            "<if test='categoryIds != null'>"+
            "AND a.category_id IN "+
            "<foreach item='item' index='index' collection='categoryIds' open='(' separator=',' close=')'>"+
            "#{params.item}"+
            "</foreach>"+
            "</if>"+
            "<if test='goodsIds != null'>"+
            "AND a.id IN "+
            "<foreach item='item' index='index' collection='goodsIds' open='(' separator=',' close=')'>"+
            "#{item}"+
            "</foreach>"+
            "</if>"+
            "GROUP BY a.id, a.name, a.list_pic_url, a.market_price, a.retail_price, a.goods_brief, b.id"+
            "<choose>"+
            "<when test='sidx != null and sidx.trim() != \"\"'> ORDER BY ${params.sidx} ${params.order} </when>"+
            "<otherwise> ORDER BY a.id DESC </otherwise>"+
            "</choose>"+
            "<if test='offset != null and limit != null'> LIMIT #{params.offset}, #{params.limit} </if>"+
            "</script>")
    List<GoodsNewDTO> queryCatalogProductList( Map<String, Object> params);
    @Insert("insert into an_nideshop_good" +
            "values" +
            "(" +
            "#{goods}," +
            ")")
    int save(GoodsNewDTO goods);
    /**
     * 批量删除操作。
     * 本方法通过接收一个整型数组来标识需要被删除的实体的ID。数组中的每个整数代表一个实体的唯一标识。
     * 方法执行后，将根据这些ID在数据库中执行相应的批量删除操作。
     *
     * @param ids 整型数组，包含需要被删除的实体的ID。数组为空或null时表示无实体需要删除。
     * @return 返回值为整型，表示本次批量删除操作影响的行数。如果返回值为0，表示没有实体被删除。
     */
    @Delete("delete from an_nideshop_good where id in (${ids})")
    int deleteBatch(Integer[] ids);
}
